/*
 * Copyright (c) 2016. André Mion
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sample.andremion.musicplayer.activities;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.format.DateUtils;
import android.widget.TextView;

import com.sample.andremion.musicplayer.R;
import com.sample.andremion.musicplayer.music.MusicControl;
import com.sample.andremion.musicplayer.view.ProgressView;

import static com.sample.andremion.musicplayer.music.MusicControl.mService;


public abstract class PlayerActivity extends AppCompatActivity {

    private TextView mTimeView;
    private TextView mDurationView;
    private ProgressView mProgressView;

    private final Handler mUpdateProgressHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            sendEmptyMessageDelayed(0, DateUtils.SECOND_IN_MILLIS);
            onUpdateProgress();
        }
    };

    protected void onUpdateProgress() {
        if (MusicControl.mService == null || !MusicControl.isPlay())
            return;
        final int position = MusicControl.getPlayPosition() / 1000;
        final int duration = MusicControl.getDuration() / 1000;
        if (position <= 0 && duration <= 0)
            return;

        if (mTimeView != null) {
            mTimeView.setText(DateUtils.formatElapsedTime(position));
        }
        if (mDurationView != null) {
            mDurationView.setText(DateUtils.formatElapsedTime(duration));
        }
        if (mProgressView != null) {
            int progress = (int) (((float) position / duration) * 100);
            mProgressView.setProgress(progress);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        super.setContentView(layoutResID);
        mTimeView = (TextView) findViewById(R.id.time);
        mDurationView = (TextView) findViewById(R.id.duration);
        mProgressView = (ProgressView) findViewById(R.id.progress);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mUpdateProgressHandler.sendEmptyMessage(0);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mUpdateProgressHandler.removeMessages(0);

    }

    public void play() {
        mService.play();
    }


}
